using System.IO.IsolatedStorage;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace PhoneApp1.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private int _count;
        public int Count
        {
            get { return _count; }
            set { _count = value; RaisePropertyChanged("Count"); }
        }

        public string PhoneNumberToCall { get; set; }

        public string ContactName { get; set; }

        public RelayCommand CallNumberCommand
        {
            get { return new RelayCommand(CallNumber, () => !string.IsNullOrEmpty(PhoneNumberToCall)); }
        }

        private void CallNumber()
        {
            var task = new Microsoft.Phone.Tasks.PhoneCallTask();
            task.DisplayName = ContactName;
            task.PhoneNumber = PhoneNumberToCall;

            task.Show();
        }

        public RelayCommand ResetCommand
        {
            get
            { return new RelayCommand(ResetCount); }
        }

        public RelayCommand IncreaseCommand
        {
            get { return new RelayCommand(IncreaseCount); }
        }

        public RelayCommand LoadCountCommand
        {
            get { return new RelayCommand(LoadCount); }
        }

        public RelayCommand SaveCountCommand
        {
            get { return new RelayCommand(SaveCount); }
        }

        private void SaveCount()
        {
            SetSetting("SavedCount", Count);
        }

        private void LoadCount()
        {
            Count = GetSetting("SavedCount");
        }

        private void IncreaseCount()
        {
            Count++;
        }

        private void ResetCount()
        {
            Count = 0;
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {


            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}
        }


        public override void Cleanup()
        {
            base.Cleanup();

            SaveCount();
        }

        private int GetSetting(string name)
        {
            var result = 0;
            IsolatedStorageSettings.ApplicationSettings.TryGetValue(name, out result);
            return result;
        }



        private void SetSetting(string name, int value)
        {
            IsolatedStorageSettings.ApplicationSettings[name] = value;
        }
    }
}